package mapper;
import com.sqsf.javabean.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.Date;
import java.util.List;
public interface UserMapper {
    List<User> selectAll();
    List<User> selectAllUser_Role();

    //根据id查询数据
    //如果返回结果为一条可以使用指定对象接收 mybatis会自动处理
    //但是注意 如果使用单个对象接收 但是sql查询结果拥有多条会报错
    User selectById(int id);

    List<User> selectByUserOne(User user);
    //根据name模糊查询
    List<User> selectLikeName(String name);
    //根据id修改name
    //增删改 仅返回影响数据条数 返回值为int
    int updateNameById(int id,String name);
    //添加一条数据
    int insert(String name, double sal, Date birthday);
    //因为对象用于存储一条数据 那么也可以使用对象内容添加数据库

    //根据int数组删除数据
    int updateByUser(User user);
    int trimTest();
    //根据数组批量删除数据
    int deleteByArray(int[] ids);
    //根据集合批量添加数据
    int insertList(List<User> users);






    @Select("select * from user")
    @Results(id = "u_r1",value = {
            @Result(id = true,column = "uid",property = "uid"),
            @Result(property = "r",column = "uid",one=@One(
                    select = "mapper.RoleMapper.selectByUid",
                    fetchType = FetchType.LAZY
            ))
    })
    List<User> selectUser_Role1();

    @Select({"<script>   select * from user\n" +
            "        <where>\n" +
            "            <if test=\"uid!=0\">and uid=#{uid}</if>\n" +
            "            <if test=\"username!=null\">and username=#{username}</if>\n" +
            "            <if test=\"password!=null\">and password=#{password}</if>\n" +
            "            <if test=\"phone!=null\">and phone=#{phone}</if>\n" +
            "        </where></script>"})
    List<User> selectByUser(User user);
    @Select("select u.*,r.* from user u left join user_role ur on u.uid=ur.uid left join role r on ur.rid=r.rid")
    @Results(id = "u_r",value = {
            @Result(id = true,column = "uid",property = "uid"),
            @Result(column = "rid",property = "r.rid"),
            @Result(column = "name",property = "r.name"),
            @Result(column = "desc",property = "r.desc")
    })
    List<User> selectUser_Role();
    @Select("select uid,username uname,password pwd,phone tel from user")
    @ResultMap("u")
    List<User> selectAllUser1();
    @Select("select uid,username uname,password pwd,phone tel from user")
    @Results(id = "u",value = {
            @Result(column = "uname",property = "username"),
            @Result(column = "pwd",property = "password"),
            @Result(column = "tel",property = "phone")
    })
    List<User> selectAllUser();
    @Update("update user set password=#{password} where uid=#{uid}")
    int updatePasswordByUid(@Param("uid") int uid,@Param("password") String password);
    @Insert("insert into user (username,password,phone)values(#{username},#{password},#{phone})")
    int insertUser(User user);

    @Delete("delete from user where uid=#{uid}")
    int deleteByUid(int uid);
    User selectByUsernameAndPassword(@Param("username") String username,@Param("password") String password);
    //mybatis注解可以简化mapper的配置 甚至可以不书写mapper

    @Select("select * from user where uid=#{uid}")
    User selectByUid(int uid);
}